package com.yidu.qianfeng.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.qianfeng.common.bean.track.TrackWaybillCondition;
import com.yidu.qianfeng.common.bean.transport.Signfor;
import com.yidu.qianfeng.common.bean.transport.TrackWaybill;
import com.yidu.qianfeng.common.util.LayuiJsonWrap;
import com.yidu.qianfeng.service.TrackWaybillService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: TangSheng
 * @Description: 运单跟踪视图层
 * @Date Created in 11:24 2021/3/29
 * @since 1.0
 */
@Controller("trackWaybillController")
public class TrackWaybillController {

    @Resource(name = "trackWaybillServiceImpl")
    private TrackWaybillService trackWaybillService;


    @RequestMapping("/addTrack")
    public @ResponseBody
    String addTrack(@RequestBody TrackWaybill trackWaybill){

        return trackWaybillService.addTrack(trackWaybill);
    }

    @RequestMapping("/deleteTrack")
    public @ResponseBody String deleteTrack(@RequestParam("trackId") String trackId){
        return trackWaybillService.deleteTrack(trackId);
    }

    @RequestMapping("/updateTrackById")
    public @ResponseBody String updateTrackById(@RequestBody TrackWaybill track){
        return trackWaybillService.updateTrackById(track);
    }

    @RequestMapping("/queryTrackLimitLayui")
    public @ResponseBody
    LayuiJsonWrap queryTrackLimitLayui(@Param("page") int page, @Param("limit") int limit){

        return trackWaybillService.queryTrackLimitLayui(page,limit);
    }

    @RequestMapping("/queryOneTrackById")
    public @ResponseBody TrackWaybill queryOneTrackById(String trackId){

        return trackWaybillService.queryOneTrackById(trackId);
    }

    @RequestMapping("/queryTrackByCondition")
    public @ResponseBody LayuiJsonWrap queryTrackByConditionLayui(@RequestParam("trackWaybillCondition") String trackWaybillCondition){
        TrackWaybillCondition trackCondition = null;
        try {
            trackCondition = new ObjectMapper().readValue(trackWaybillCondition, TrackWaybillCondition.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return trackWaybillService.queryTrackByConditionLayui(trackCondition);
    }

    
    @RequestMapping("/queryTrackNoSign")
    public @ResponseBody LayuiJsonWrap queryTrackNoSign(@Param("page") int page, @Param("limit") int limit){
        return trackWaybillService.queryTrackNoSign(page,limit);
    }

    /**
     * 查询已签收的订单信息
     * @param page 页面
     * @param limit 页目
     * @return map集合对象
     */
    @RequestMapping("/selectSignforPaing")
    public @ResponseBody
    Map selectSignforPaing(int page, int limit){

        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",trackWaybillService.selectSignforCount());
        map.put("data",trackWaybillService.selectSignforPaing((page-1)*limit,limit));

        return map;
    }

    /**
     * 根据托运单号搜索跟踪表数据
     * @param trackConsignNum 托运单号
     * @return map集合
     */
    @RequestMapping("/bytrackConsignNumSelect")
    public @ResponseBody Map bytrackConsignNumSelect(String trackConsignNum){

        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",trackWaybillService.bytrackConsignNumSelect(trackConsignNum));
        map.put("data",trackWaybillService.bytrackConsignNumSelect(trackConsignNum));

        return map;
    }

    /**
     * 查询派送中的托运单号
     * @return 结果集合对象
     */
    @RequestMapping("/getTrackConsignNum")
    public @ResponseBody
    List<TrackWaybill> getTrackConsignNum(){
        List<TrackWaybill> trackConsignNum = trackWaybillService.getTrackConsignNum();

        return trackConsignNum;
    }

    /**
     * 根据托运单号查找到达站司机
     * @param trackConsignNum
     * @return
     */
    @RequestMapping("/byTrackConsignNumQueryDriver")
    public @ResponseBody
    List<TrackWaybill>
    byTrackConsignNumQueryDriver(String trackConsignNum){
        List<TrackWaybill> trackWaybills = trackWaybillService.byTrackConsignNumQueryDriver(trackConsignNum);

        return trackWaybills;
    }

    /**
     * @Title: uploadSource
     * @Description: 文件上传，返回文件的存储路径
     * @param file
     * @return Object
     * @author hzp
     * @date 2019年5月6日下午9:51:16
     */
    @RequestMapping(value="/uploadFile")
    @ResponseBody
    public String uploadSource(@RequestParam("file") MultipartFile file , HttpServletRequest request) {
        String pathString = null;
        if(file!=null) {
            pathString = "D:/upload/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" +file.getOriginalFilename();
        }
        try {
            File files=new File(pathString);
            //打印查看上传路径
            System.out.println(pathString);
            if(!files.getParentFile().exists()){
                files.getParentFile().mkdirs();
            }
            file.transferTo(files);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "{\"code\":0,\"msg\":\""+pathString+"\"}";
    }

    /**
     * 新增签收表并且修改运单跟踪表跟踪类型为7
     * @param trackConsignNum 运单追踪表主键
     * @param trackDate 更新日期
     * @param trackStatus 签收状态
     * @param signDriver 签收司机
     * @param trackReceiptPhotoUrl 签收照片路径
     */
    @RequestMapping("/addSignfor")
    public @ResponseBody String
    addSignfor(String trackConsignNum,String trackDate,String trackStatus,String signDriver,String trackReceiptPhotoUrl){
        String s = UUID.randomUUID().toString();

        Signfor signfor = new Signfor(s,trackConsignNum,trackDate,signDriver);
        int b = trackWaybillService.updatetrackType(trackConsignNum,trackStatus,trackReceiptPhotoUrl);

        return trackWaybillService.addSignfor(signfor);
    }
}
